package com.wkr.demo;

import java.util.LinkedList;
import java.util.List;

public class LambdaTest {
    // 类型声明
    MathOperation addition = (int a, int b) -> a + b;

    // 不用类型声明
    MathOperation subtraction = (a, b) -> a - b;

    // 大括号中的返回语句
    MathOperation multiplication = (int a, int b) -> {
        return a * b;
    };

    // 没有大括号及返回语句
    MathOperation division = (int a, int b) -> a / b;

    public static void main(String args[]) {
        LambdaTest tester = new LambdaTest();


        System.out.println("10 + 5 = " + tester.operate(10, 5, tester.addition));
        System.out.println("10 - 5 = " + tester.operate(10, 5, tester.subtraction));
        System.out.println("10 x 5 = " + tester.operate(10, 5, tester.multiplication));
        System.out.println("10 / 5 = " + tester.operate(10, 5, tester.division));

        // 不用括号
        GreetingService greetService1 = message ->
                System.out.println("Hello " + message);

        // 用括号
        GreetingService greetService2 = (message) ->
                System.out.println("Hello " + message);

        greetService1.sayMessage("Runoob");
        greetService2.sayMessage("Google");

        List<String> list = new LinkedList<>();
        list.add("1");
        list.add("2");
        list.forEach((String p) -> {System.out.println(p);});
    }

    interface MathOperation {
        int operation(int a, int b);
    }

    interface GreetingService {
        void sayMessage(String message);
    }

    private int operate(int a, int b, MathOperation mathOperation) {
        return mathOperation.operation(a, b);
    }
}
